package com.markerhub.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.markerhub.common.lang.Result;
import com.markerhub.entity.SysShop;
import com.markerhub.entity.SysUser;
import com.markerhub.service.SysShopService;
import com.markerhub.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.Date;
import java.util.List;

/**
 * @author jsx on 2023/2/2
 **/
@RestController
@RequestMapping("/sys/shop")
public class SysShopController extends  BaseController{

    @Autowired
    private SysShopService shopService;

    //增加
    @RequestMapping("/save")
    public Result save(@RequestBody SysShop shop, Principal principal){
        shop.setCreater(principal.getName());
        shop.setCreateTime(DateUtils.dateToFormat(new Date()));
        shopService.save(shop);
        return Result.succ("保存成功");
    }

    //修改
    @RequestMapping("/update")
    public Result update(@RequestBody SysShop sysShop,Principal principal){
        sysShop.setUpdater(principal.getName());
        sysShop.setUpdateTime(DateUtils.dateToFormat(new Date()));
        shopService.updateById(sysShop);
        return  Result.succ("更新成功");
    }

    //查询
    @RequestMapping("/list")
    public Result list(@RequestParam(required = false) String name ,Integer current, Integer size,Principal principal){
        Page<SysShop> list = shopService.getList(getPage(current,size),name);
        return Result.succ(list);
    }

    //查询单条记录
    @RequestMapping("/info/{id}")
    public Result getInfo(@PathVariable("id") String id){
        SysShop shop = shopService.getById(id);
        return Result.succ(shop);
    }

    //删除
    @RequestMapping("/delete")
    public Result delete(String id){
        shopService.removeById(id);
        return Result.succ("删除完成");
    }
}
